package com.wind.web.controller.system;

import com.wind.common.config.RuoYiConfig;
import com.wind.common.utils.file.FileUploadUtils;
import com.wind.common.utils.file.MimeTypeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wind.common.core.controller.BaseController;
import com.wind.common.core.domain.AjaxResult;
import com.wind.common.core.domain.model.RegisterBody;
import com.wind.common.utils.StringUtils;
import com.wind.framework.web.service.SysRegisterService;
import com.wind.system.service.ISysConfigService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/register")
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @PostMapping
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    // 注册头像上传 TODO
    @PostMapping("/avatar")
    public AjaxResult registerAvatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }
}
